package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.exception.BaseException;
import com.ruoyi.common.core.utils.ConvertUtil;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.LanguageType;
import com.ruoyi.system.domain.params.LanguageKeyParams;
import com.ruoyi.system.domain.params.LanguageTypeParams;
import com.ruoyi.system.domain.vo.LanguageKeyVo;
import com.ruoyi.system.domain.vo.LanguageTypeVo;
import com.ruoyi.system.domain.vo.LanguageValueVo;
import com.ruoyi.system.mapper.LanguageKeyMapper;
import com.ruoyi.system.mapper.LanguageTypeMapper;
import com.ruoyi.system.mapper.LanguageValueMapper;
import com.ruoyi.system.service.ILanguageTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author qy
 * @date 2023/3/15 14:35
 */
@Service
public class LanguageTypeServiceImpl implements ILanguageTypeService {

    @Autowired
    private LanguageTypeMapper languageTypeMapper;

    @Autowired
    private LanguageValueMapper languageValueMapper;

    @Autowired
    private LanguageKeyMapper languageKeyMapper;

    @Override
    public IPage<LanguageTypeVo> queryList(IPage<LanguageTypeVo> page, LanguageTypeParams languageTypeParams) {
        return languageTypeMapper.queryList(page, languageTypeParams, null);
    }

    @Override
    @Transactional
    public void saveLanguageType(LanguageType languageType, String createBy) {
        LanguageType byObj = this.getByCode(languageType.getCode());
        if (byObj != null) {
            throw new BaseException("\"该语言cod已存在\"");
        }
        this.updateIsDefault(languageType);
        languageType.setCreateBy(SecurityUtils.getUsername());
        languageType.setCreateTime(DateUtils.getNowDate());
        this.save(languageType);
        //初始化所有key
        //获取现有所有语言key
        List<LanguageKeyVo> languageKeyVos = languageKeyMapper.queryList(new LanguageKeyParams());
        if (languageKeyVos.size() > 0) {
            List<LanguageValueVo> languageValueVoList = ConvertUtil.sourceToTarget(languageKeyVos, LanguageValueVo.class);
            languageValueMapper.insertList(languageValueVoList, createBy, languageType.getCode());
        }
    }

    @Override
    public int updateLanguageType(LanguageType languageType) {
        this.updateIsDefault(languageType);
        if (languageType.getCode() != null && languageType.getCode() != "") {
            LanguageType query = new LanguageType();
            query.setCode(languageType.getCode());
            query.setId(languageType.getId());
            query.setApplicationCode(languageType.getApplicationCode());
            LanguageType byObj = this.getByObjForUpdateDefault(query);
            if (byObj != null) {
                throw new BaseException("该语言code已存在");
            }
        }
        languageType.setUpdateBy(SecurityUtils.getUsername());
        languageType.setUpdateTime(DateUtils.getNowDate());
        return this.update(languageType);
    }

    @Override
    public LanguageType queryLanguageTypeByCode(String code) {
        return null;
    }

    @Override
    public int deleteByIds(Long[] ids) {
        return languageTypeMapper.deleteByIds(ids);
    }

    @Override
    public void updateIsDefault(LanguageType languageType) {
        if ("Y".equals(languageType.getIsDefault())) {
            LanguageType query = new LanguageType();
            query.setIsDefault("Y");
            query.setIsDelete("N");
            query.setId(languageType.getId());
            query.setApplicationCode(languageType.getApplicationCode());
            LanguageType byObj = this.getByObjForUpdateDefault(query);
            if (byObj != null) {
                LanguageType languageType1 = new LanguageType();
                languageType1.setId(byObj.getId());
                languageType1.setIsDefault("N");
                languageType1.setApplicationCode(byObj.getApplicationCode());
                this.update(languageType1);
            }
        }
    }

    @Override
    public LanguageType getById(Long id) {
        return languageTypeMapper.getById(id);
    }

    @Override
    public LanguageType getByCode(String code) {
        return languageTypeMapper.getByCode(code);
    }

    @Override
    public int save(LanguageType languageType) {
        int save = languageTypeMapper.save(languageType);
        return save;
    }

    @Override
    public LanguageType getByObjForUpdateDefault(LanguageType query) {
        return languageTypeMapper.getByObjForUpdateDefault(query);
    }

    @Override
    public int update(LanguageType languageType) {
        return languageTypeMapper.update(languageType);
    }

    @Override
    public LanguageType getLanguageByCode(String codeType,String applicationCode) {
        //先获取code
        LanguageType query = new LanguageType();
        query.setCode(codeType);
        query.setIsDelete("N");
        query.setApplicationCode(applicationCode);
        LanguageType languageType = languageTypeMapper.selectOne(query);
        if (languageType != null) {
            return languageType;
        }

        //wrapper.like(LanguageType::getCodes, codeType + ";").eq(LanguageType::getIsDelete, SysYesNoEnum.NO.getCode());
        List<LanguageType> languageTypes = languageTypeMapper.selectByCodes(codeType+";");
        if (languageTypes.size() > 0) {
            return languageTypes.get(0);
        }

        query = new LanguageType();
        query.setApplicationCode(applicationCode);
        query.setIsDefault("Y");
        query.setIsDelete("N");
        //wrapper.eq(LanguageType::getIsDefault, SysYesNoEnum.YES.getCode()).eq(LanguageType::getIsDelete, SysYesNoEnum.NO.getCode());
        languageType = languageTypeMapper.selectOne(query);
        if (languageType != null) {
            return languageType;
        }

        return null;
    }
}
